package is.ru.honn.rupin.service;

import is.ru.honn.rupin.data.BoardDataGateway;
import is.ru.honn.rupin.data.PinDataGateway;
import is.ru.honn.rupin.data.UserDataGateway;
import is.ru.honn.rupin.domain.Board;
import is.ru.honn.rupin.domain.Gender;
import is.ru.honn.rupin.domain.Pin;
import is.ru.honn.rupin.domain.User;
import is.ruframework.data.RuDataAccessFactory;
import is.ruframework.domain.RuException;

import java.util.List;

/**
 * Created by Sigrún Inga Kristinsdóttir.
 * Date: 20.10.2012
 * Time: 13:20
 * copyright (c) sigruninga&susanne
 *
 * @author Sigrún Inga kristindóttir og Susanne Götz
 * @version 1.1
 */
public class PinServiceData implements PinService
{
    /**
     * Breytur sem PinServiceData þarf að nota
     */
    public RuDataAccessFactory factory;
    public UserDataGateway userDataGateway;
    public BoardDataGateway boardDataGateway;
    public PinDataGateway pinDataGateway;
    //Breytur end

    /**
     * Færibreytulaus smiður sem nær í gögn frá data.xml.
     * Ef xml skjalið finnst ekki þá kastar hann villu.
     */
    public PinServiceData()
    {
        try
        {
            factory = RuDataAccessFactory.getInstance("data.xml");
        }
        catch (RuException e)
        {
            System.out.println("unable to load");
            System.exit(1);
        }

        userDataGateway = (UserDataGateway)factory.getDataAccess("userDataAccess");
        boardDataGateway = (BoardDataGateway)factory.getDataAccess("boardDataAccess");
        pinDataGateway = (PinDataGateway)factory.getDataAccess("pinDataAccess");
    }

    /**
     * Fall sem býr til nýjan User og skráir hann í gagnagrunn
     * @param username
     * @param firstName
     * @param lastName
     * @param email
     * @param password
     * @param gender
     * @return User sem verið var að búa til
     * @throws UsernameExistsException
     */
    public User signUpUser(String username, String firstName, String lastName, String email,
                           String password, Gender gender) throws UsernameExistsException {
        {
            if(userDataGateway.getUserByUsername(username) != null)
                throw new UsernameExistsException("Notandi er þegar til");
            User newUser = new User(username, firstName, lastName, email, password, gender);
            userDataGateway.add(newUser);
            return newUser;
        }
    }

    /**
     * Fall sem sækir notanda með notandanafn "username" úr gagnagrunn
     * @param username
     * @return notanda með notandanafn "username"
     */
    public User getUser(String username)
    {
        return userDataGateway.getUserByUsername(username);
    }

    /**
     * Fall sen sækir borð tengd notanda "username" og boardname "boardname" úr gagnagrunni
     * @param username
     * @param boardname
     * @return Borð sem tengist bæði notanda og boardnafni
     */
    public Board getBoard(String username, String boardname)
    {
        return boardDataGateway.getBoardByKey(boardname, username);
    }

    /**
     * Fall sem sækir öll borð sem tengjast notanda með notandanafn "username"  úr gagnagrunni
     * @param username
     * @return Lista af borðum
     */
    public List<Board> getBoards(String username)
    {
       return boardDataGateway.getBoardsByUser(username);
    }

    /**
     * Fall sem býr til nýtt borð og skráir það í gagnagrunn
     * @param username
     * @param boardname
     * @param category
     * @return Borð sem verið var að búa til
     * @throws UserNotFoundException
     */
    public Board createBoard(String username, String boardname, String category) throws UserNotFoundException
    {
       User user = userDataGateway.getUserByUsername(username);
        if(userDataGateway == null)
            throw new UserNotFoundException("user not found");     // message for testing

        Board newBoard = new Board(boardname, category);
        newBoard.setCreator(userDataGateway.getUserByUsername(username));
        boardDataGateway.add(newBoard);
        return newBoard;
    }

    /**
     * Fall sem býr til Pin sem tengist vissu borði og notanda og skráir það í gagnagrunn
     * @param username
     * @param boardname
     * @param link
     * @param description
     * @return  Pin sem verið var að búa til
     * @throws BoardNotFoundException
     */
    public Pin createPin(String username, String boardname, String link, String description) throws BoardNotFoundException
    {
        Pin newPin = new Pin();
        boardDataGateway.getBoardByKey(boardname, username);
        if(null == boardDataGateway)
            throw new BoardNotFoundException("Board does not exist");
        newPin.setBoard(boardDataGateway.getBoardByKey(boardname, username));
        newPin.setLink(link);
        newPin.setDescription(description);
        newPin.setCreator(userDataGateway.getUserByUsername(username));
        pinDataGateway.add(newPin);
        return newPin;
    }


    /**
     * Fall sem sækir öll pins sem eru á borði sem tengist notanada með notandanafn "username"
     * og er með nafni "boardname" úr gagnagrunni
     * @param username
     * @param boardname
     * @return Lista af pinnum
     */
    public List<Pin> getPinsOnBoard(String username, String boardname)
    {
        Board board = this.getBoard(username,boardname);
        if (board != null)
        {
             return pinDataGateway.getPinsByBoard(boardname);
        }
        return null;
    }
}
